package cn.bonoon.kernel.web.handlers.loader;

import java.lang.reflect.Field;
import java.util.List;

import cn.bonoon.handler.AutoManager;
import cn.bonoon.kernel.events.ReadEvent;
import cn.bonoon.kernel.support.models.AjaxNode;
import cn.bonoon.kernel.support.searcher.Finder;
import cn.bonoon.kernel.support.searcher.StandardFinder;
import cn.bonoon.kernel.web.annotations.AutoDataLoader;

/**
 * <pre>
 * 只允许使用{@link cn.bonoon.kernel.support.models.AjaxNode AjaxNode}，支持单选和多选，不建议使用
 * </pre>
 * @author jackson
 * @see BaseComboAutoLoaderHandler
 * @see ComboAutoLoaderHandler
 * @see AbstractAutoLoaderHandler
 * 
 * @see ComboBoxAutoLoaderHandler
 * @see ComboTreeAutoLoaderHandler
 * @see ComboGridAutoLoaderHandler
 */
public class ComboTreeAjaxAutoLoaderHandler extends BaseComboAutoLoaderHandler{

	public ComboTreeAjaxAutoLoaderHandler(AutoManager manager, AutoDataLoader loader, Field field) throws Exception {
		super(manager, null, loader, field);
	}

	@Override
	protected List<AjaxNode> __parseResult(Finder finder, ReadEvent event) {
		if(isMultiple()){
			return finder.ajaxNodes(selectFields, new SelectedFilter(event));
		}
		return finder.ajaxNodes(selectFields);
	}
	@Override
	protected void parseQuery(StandardFinder finder, ReadEvent event) throws Exception {
		super.parseQuery(finder, event);
		Long pid = event.getLong("id");
		if(pid != null && pid > 0){
			finder.and("x.parent.id=?", pid);
		}else{
			finder.and("x.parent is null");
		}
	}
}
